package com.shp.service.serviceImpl;

import com.shp.dao.ShopCartDao;
import com.shp.pojo.ShopCartItem;
import com.shp.service.ShopCartService;
import com.shp.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Description:
 *
 * @version 1.0
 * @author zwl
 * @date 2020/2/27 23:03
 */
@Service(value = "shopCartService")
public class ShopCartServiceImpl implements ShopCartService {

    @Autowired
    private ShopCartDao shopCartDao;

    /**
     * 根据用户的id，查询他的购物车信息
     *
     * @param id 用户id
     * @return List<ShopCartItem>
     */
    @Override
    public List<ShopCartItem> getShopCartByUserId(Integer id) {
        return shopCartDao.getShopCartByUserId(id);
    }

    /**
     * 获取购物车的商品数量
     *
     * @param userId 用户id
     * @return 数量
     */
    @Override
    public int getShopCartItemSize(Integer userId) {
        return shopCartDao.getShopCartItemSize(userId);
    }

    /**
     * 移除购物车里的商品项
     *
     * @param id 商品id
     * @return
     */
    @Override
    public boolean removeShopCartItem(Integer id) {
        if(shopCartDao.removeShopCartItemByGoodsId(id, DateUtil.getNowTime()) > 0){
            return true;
        }
        return false;
    }

    /**
     * 清空用户的购物车
     *
     * @param userId
     * @return
     */
    @Override
    public boolean empty(Integer userId) {
        if(shopCartDao.emptyByUserId(userId, DateUtil.getNowTime()) > 0){
            return true;
        }
        return false;
    }
}
